//
//  OBDBluetoothParseDataTools.swift
//  OBD
//
//  Created by apple on 2021/11/10.
//

import UIKit

class OBDBluetoothParseDataTools: NSObject {
    /// 应答消息返回解析
    /// - Parameter bytes: 总消息数据
    /// - Returns:OBDMsgResponse实体类
    static func createOBDResponse(bytes:[UInt8]) -> OBDMsgResponse {
        var msgResponse = OBDMsgResponse()
        msgResponse.bytes = bytes
        /// 标识位
        let identifier = bytes[..<2]
        msgResponse.identifier = identifier.hexString

        
        /// 校验码
        let check = bytes[bytes.count-1..<bytes.count]
        msgResponse.check = check.hexString
        
        /// 消息头
        let msgID = bytes[start: 3, length: 2]
        let encrypt = bytes[start: 5, length: 1]
        let length = bytes[start:6, length: 1]
        let frame = bytes[start: 7, length: 2]
        
        var msgHeader = OBDMsgHeader()
        msgHeader.ID = msgID.hexString
        msgHeader.encrypt = encrypt.hexString
        msgHeader.length = length.hexString
        msgHeader.frame = frame.hexString
        msgHeader.bytes = bytes[identifier.endIndex..<frame.endIndex]
        msgResponse.header = msgHeader

        /// 消息体
        let content = bytes[frame.endIndex..<bytes.count-1]
        var msgContent = OBDMsgResponseContent()
        let number = bytes[start:content.startIndex, length: 2]
        let ID = bytes[start:number.endIndex, length: 2]
        let res = bytes[ID.endIndex..<content.endIndex]
        
        
        /// 消息体参数解析
        var paramArr:[OBDMsgParam] = []
        var last:ArraySlice<UInt8> = number
        let count = number.hexToInt
        for _ in 0..<count! {
            var msgParam = OBDMsgParam()
            let startIndex = last.endIndex
            let paramID = bytes[start:startIndex,length:2]
            
            let paramCount = bytes[start:startIndex+2,length:1]
            msgParam.command = paramID.hexString
            msgParam.count = 1
            msgParam.value = paramCount.hexString
            msgParam.totalByte = bytes
            msgParam.bytes = bytes[paramID.endIndex..<paramCount.endIndex]

            last = paramCount
            if msgParam.command!.count > 0 {
                paramArr.append(msgParam)
            }
            if last.endIndex == check.startIndex {
                break
            }
        }
        
        msgContent.ID = ID.hexString
        msgContent.number = number.hexString
        msgContent.res = res.hexString
        msgContent.bytes = content
        msgResponse.content = msgContent
        msgContent.params = paramArr
        
        return msgResponse
    }

    
    /// 普通消息协议返回
    /// - Parameter bytes: 总消息数据
    /// - Returns: OBDProtocol实体类
	static func createOBDProtocol(bytes:[UInt8]) -> OBDProtocol {
        
		var msgProtocol = OBDProtocol()
		
		/// 标识位
		let identifier = bytes[..<2]
		if identifier[0] == 0x55 && identifier[1] == 0xAA {
			msgProtocol.identifier = identifier.hexString
		}
		
		/// 消息头
        let encrypt = bytes[start: 2, length: 1]
		let msgID = bytes[start: 3, length: 2]
		let length = bytes[start:5, length: 2]
		let frame = bytes[start: 7, length: 2]
        		
		var msgHeader = OBDMsgHeader()
        msgHeader.ID = msgID.hexString
        msgHeader.encrypt = encrypt.hexString
        msgHeader.length = length.hexString
		msgHeader.frame = frame.hexString
        msgHeader.bytes = bytes[identifier.endIndex..<frame.endIndex]
        msgProtocol.header = msgHeader
        
        /// 校验码
        let check = bytes[bytes.count-1..<bytes.count]
        let checkCode = check.hexString
        
		/// 消息体 暂时这样标识应答返回数据解析判断............
        var countFromIndex = frame.endIndex
        if msgID.hexString?.toInt16 != response_protocol.DEVICE_COMMON_RESP_REALTIME.rawValue {
            countFromIndex += 2
        }
        
		let contentCount = bytes[start:countFromIndex, length: 2]
        let paramsContent = bytes[contentCount.endIndex..<bytes.count-1]

		var msgContent = OBDMsgContent()
        let count = contentCount.hexToInt
		msgContent.count = count
		msgContent.content = paramsContent.hexString
        msgContent.bytes = bytes[frame.endIndex..<paramsContent.endIndex]

        /// 消息体参数解析
        var paramArr:[OBDMsgParam] = []
        var last:ArraySlice<UInt8> = contentCount
        for _ in 0..<count! {
            var msgParam = OBDMsgParam()
            let startIndex = last.endIndex
            let paramID = bytes[start:startIndex,length:2]
            let paramCount = bytes[start:startIndex+2,length:1]
            let paramCountInt = paramCount.hexToInt
            let value = bytes[start:paramCount.endIndex,length:paramCountInt!]
            
            msgParam.command = paramID.hexString
            msgParam.count = paramCountInt
            msgParam.value = value.hexString
            if value.count > 0 {
                msgParam.bytes = bytes[paramID.endIndex..<value.endIndex]
            }
            msgParam.totalByte = bytes
                    
            last = value
            if msgParam.command!.count > 0 {
                paramArr.append(msgParam)
            }
            
            if last.endIndex == check.startIndex {
                break
            }
        }
        
        /// 标识位 消息头 消息体 校验码赋值
        msgProtocol.content = msgContent
        msgProtocol.check = checkCode
        msgProtocol.content?.params = paramArr
		
		return msgProtocol
	}
}
